package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.EGetById;
import com.itheima.entity.Employee;
import com.itheima.exception.loginException;
import com.itheima.jsonresult.JsonResult;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import com.itheima.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author linying
 * @since 2022-10-13
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private RedisTemplate redisTemplate;



    /**
     * 新增员工
     * @param employee
     * @return
     */
    @Override
    public JsonResult add(Employee employee) {
        //根据username查询是否存在
        QueryWrapper wrapper= new QueryWrapper();
        wrapper.eq("username",employee.getUsername());

        //查询结果为空，不存在
        if ((getOne(wrapper)==null)){
            //添加数据并响应
            save(employee);
            return new JsonResult(1,"添加成功",null,null);
        }else {
            return new JsonResult(0,employee.getUsername()+"已存在",null,null);
        }
    }

    /**
     * 启用/禁用/修改
     * @param employee
     * @return
     */
    @Override
    public JsonResult put(Employee employee) {
        //添加查询的条件username
        QueryWrapper wrapper= new QueryWrapper();
        wrapper.eq("username",employee.getUsername());

        //根据username查询结果为空，不存在
        if ((getOne(wrapper)==null)){
            //响应数据不存在
            return new JsonResult(0,null,"员工信息不存在",null);
        }else {
            //存在，根据username查询，并修改数据
            update(employee,wrapper);
            return new JsonResult(1,null,"员工信息修改成功",null);
        }
    }

    /**
     * 员工登录
     * @param employee
     * @return
     */
    @Override
    public JsonResult login(Employee employee) {
        // 设置判断条件
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", employee.getUsername());
        wrapper.eq("password", employee.getPassword());

        // sql获取到的对象
        Employee one = getOne(wrapper);

        if (one == null) {
            throw new loginException();
        }

        // 新建一个emp对象，复制获取到的对象的内容
        Employee employeevo = new Employee();
        BeanUtil.copyProperties(one, employeevo);

        // 要创建token必须要有一个map，map的内容放入对象信息
        Map<String, String> map = new HashMap<>();
        map.put("employee", JSON.toJSONString(employeevo));
        String token = JwtUtils.getToken(map);

        // 把token存入emp对象
        employeevo.setToken(token);

        // 把token存入redis数据库           key-token信息   value-该对象的信息
        redisTemplate.opsForValue().set(token, JSON.toJSONString(employeevo), 1, TimeUnit.DAYS);

        //响应相应数据给前端
        return JsonResult.success(employeevo);
    }

    /**
     * 员工登出
     * @param request
     * @return
     */
    @Override
    public JsonResult logout(HttpServletRequest request) {
        // 获取浏览器的token
        String token = request.getHeader("Authorization");
        // 删除仓库里响应的token
        redisTemplate.delete(token);

        return JsonResult.success(null);
    }

    /**
     * 员工分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult getPage(String name, Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        //姓名可选非空条件判断
        if (!StrUtil.isBlank(name)) {
            wrapper.eq("name", name);
        }
        //调用分页查询方法
        Page jsonpage = (Page) page(new Page<>(page, pageSize), wrapper);

        return JsonResult.success(jsonpage);
    }

    /**
     * 根据id查询员工接口
     * @param id
     * @return
     */
    @Override
    public JsonResult getEmployeeById(String id) {
        //调用根据id查询的方法
        Employee emp = getById(id);
        //创建vo类，复制查询结果进去
        EGetById eGetById =  new EGetById();
        BeanUtil.copyProperties(emp, eGetById);

        //返回结果
        return JsonResult.success(eGetById);
    }


}
